<?php


class chat
{
var $post;
var $data;
var $where;
var $user_id;
var $user_level;
var $lines;


function __construct($userlevel,$userid) 
{
	$this->user_id = $userid;
	$this->user_level = $userlevel;
	global $conf;
	if($conf->chatmode)
	{
			$this->lines = 20;//to change number of maximum lines change number
				$this->user_level =10;
	}
	else
	{
			$this->lines = 8;//to change number of maximum lines change number
	}

}
	
function message_send()
{
		
	$this->data['logged_in']=true;
	$this->post['date'] = date('Y-m-d');
	$this->post['time'] = date('H:i');	
	$this->post['content'] = (int)$_POST['content_id'];
	$this->post['user_id']=$this->user_id; 	
	
	//strip tags and special characters----------------- for username
	
	$this->post['name'] = $this->rep_string(htmlentities(strip_tags($_POST['name'])));
	
	
	$this->post['message'] =$this-> rep_string(htmlentities(strip_tags($_POST['message']), ENT_QUOTES));
	$this->post['message'] = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $this->post['message']);
	$this->post['message'] = $this->smiley($this->post['message']);
	
	
	$this->data['logged_in'] = true;
	if(!$this->nickname_exists($this->post['name'])){
		$this->post['name'] = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $this->post['name']);
		process_sql_post($this->post,'message_log','add',NULL,NULL);
	}
	
}

function message_update()
{
	
		
		
		
		$this->post['name'] = $this->rep_string(htmlentities(strip_tags($_POST['name'])));
		
		
		
			if($this->nickname_exists($this->post['name']) && $this->post['name']!="")
			{
					$this->data['nickname_exists'] = true;

			}
			else
			{
				
					$this->update_nickname($this->post['name']);		
					$this->data['nickname_exists'] = false;
			}
		
		
		$o['order'][0]="id";
		$o['order'][1]="DESC";
		$o['limit'][0]=0;
		$o['limit'][1]=$this->lines;
		$w[0]='content';
		$w[1]=$_POST['content_id'];
		$w[2]='=';
		$where[] = $w;
		
		$ij[0]=TRUE;
		$ij[1]='user';
		$ij[2][0]='user_level';
		$ij[3][0]='user_id';
		$ij[3][1]='user_id';
		
		$row=get_sql('message_log','id',$where,$o,$ij);
		$r = current($row);
		$r2 = count($row);
		$rows = $r['id'];
		
		if($_POST['maxid']==$rows)
			$this->data['state']="waiting";
		

		else
		{
						
		
			unset($o);	
			
		
			if($_POST['maxid']==0)
			{
					
				$diff = $this->lines;	
				$d = $rows-$this->lines;
				$this->data['diff']=0;
				$this->data['initialize'] = true;
			}
					
			else
			{
				$diff=$rows-$_POST['maxid']; 
				$d = $rows-$diff;
				$this->data['diff']=$diff;
				$this->data['initialize'] = false;
			}
		
			unset($o);	
				
			$this->data['text']="";
			
			foreach($row as $c)
				{
					
					$level = $c['user_level'];
					
					if($level<10){
						$this->data['text'] .= "<li> <h4>".$c['time']."</h4> <h6>".$c['name']."<h6> : <h5>".$c['message']."</h5></li>";
					
					}
					else{
						$this->data['text'] .= "<li> <h4>".$c['time']."</h4> <h7>".$c['name']."</h7> : <h5>".$c['message']."</h5></li>";      
					
						}
				
				}
			$this->data['state']="update"; 
			$this->data['maxid']=$rows;
			$this->data['lines']=$r2;  
			
			
		}  
}


function set_logged_in_false()
{
		$this->data['logged_in']=false; 	  	
}
function get_max_id()
{
		
		$this->data['logged_in']=true;
		$o['order'][0]="id";
		$o['order'][1]="DESC";
		$o['limit'][0]=0;
		$o['limit'][1]=1;
		
		$row=get_sql('message_log','id',$where,$o);
		$c1=current($row);
		$maxid=$c1['id'];
		$this->data['maxid'] = $maxid;	
}
 
function smiley($msg) { 
      
      $msg = ereg_replace("\:\)\)","<img src=assets/images/emoticons/g.png height=15 width=15>", $msg); 
      $msg = ereg_replace("\:\)","<img src=assets/images/emoticons/k.png height=15 width=15>", $msg); 
      $msg = ereg_replace("\:\D","<img src=assets/images/emoticons/b.png height=15 width=15>", $msg);
      $msg = ereg_replace("\:\(","<img src=assets/images/emoticons/j.png height=15 width=15>", $msg);
      $msg = ereg_replace("\B\)","<img src=assets/images/emoticons/d.png height=15 width=15>", $msg);
      $msg = ereg_replace("\:\p","<img src=assets/images/emoticons/l.png height=15 width=15>", $msg);
      $msg = ereg_replace("\;\)","<img src=assets/images/emoticons/m.png height=15 width=15>", $msg);
      $msg = ereg_replace("\:o","<img src=assets/images/emoticons/h.png height=15 width=15>", $msg);
     
      return $msg; 
} 
 
function nickname_exists($name)
{ 
		
		$w[0]='user_nickname';
		$w[1]=strtoupper($name);
		$w[2]='=';
		$where[0]=$w;
				
		$w[0]='user_id';
		$w[1]=$this->user_id;
		$w[2]='!=';
		$where[1]=$w;
		
		
		$exists = get_sql('user','user_id',$where,$o);
		
		if(isset($exists))
			return true;
		else
			return false; 
}

function update_nickname($name)
{
			
			$this->post['user_nickname'] = strtoupper($name);
			process_sql_post($this->post,'user','update','user_id',$this->user_id);
			
			
}
function remove_nickname()
{
			$this->post['user_nickname']="";
			process_sql_post($this->post,'user','update','user_id',$this->user_id);	 		
}
function show_history()
{     
		unset($o);
		unset($where);
		
		$w[0]='content';
		$w[1]=$_POST['content_id'];
		$w[2]='=';
		$where[0] = $w;
		$o['order'][0]="id";
		$o['order'][1]="DESC";
		$ij[0]=TRUE;
		$ij[1]='user';
		$ij[2][0]='user_level';
		$ij[3][0]='user_id';
		$ij[3][1]='user_id';
		
		$row=get_sql('message_log','id',$where,$o,$ij);
		if(!$row )
		{
			die('Could not get data: ' . mysql_error());
		}
		$rows = count($row);
		
		if($rows>$this->lines){
				unset($o);
				unset($where);
				
				if($rows>(30+$this->lines)){
					$d=$rows-(30+$this->lines);
					$row = array_slice($row, $this->lines,30);	
				}
				else
				{
					$d=$this->lines;
					$row = array_slice($row, $this->lines,$rows-$this->lines);
				}
				
				
				
				$this->data['text']="";
				
				foreach($row as $c)
				{
				
						$level = $c['user_level'];
					
						
					if($level<10)
						$this->data['text'] .= "<li> <h4>".$c['time']."</h4> <h6>".$c['name']."<h6> : <h5>".$c['message']."</h5></li>";
					else
						$this->data['text'] .= "<li> <h4>".$c['time']."</h4> <h7>".$c['name']."</h7> : <h5>".$c['message']."</h5></li>";      
					
				
				}
		}
		else
		{
		$this->data['text'] .= "<li><h5>There are no previous messages</h5></li>";
		}
		$this->data['maxid']=$rows;
			
}
	function rep_string($msg)
{
		$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
	
	
		//badwords---------------------------------------------------------------------------------------------------------
		$badwords = array("fuck", "fucker", "shit", "bitch", "puta","tangina","tite","suso", "gago", "kupal","tado", "putangina", "taena", "pekpek","puke","punyeta","punieta", "salsal", "bobo");
		$replacement = "[ badword blocked ]";
		if (preg_match($reg_exUrl, $msg, $url))//replace url with link
						$msg = preg_replace($reg_exUrl, '<a href="'.$url[0].'" target="_blank">'.$url[0].'</a>', $msg);
	
	
			//filter out the bad words--------------------------------------------------------------------

			for($i=0;$i < sizeof($badwords);$i++)
			$msg=eregi_replace($badwords[$i], $replacement, $msg);
		
	
		return $msg;  
	}
}
?>